//
//  NSDictionary+PropertyCode.m
//  07-Runtime(字典转模型KVC实现)
//
//  Created by xmg on 16/1/11.
//  Copyright © 2016年 xiaomage. All rights reserved.
//

#import "NSDictionary+PropertyCode.h"

// isKindOfClass:判断下是否是当前类或者子类

@implementation NSDictionary (PropertyCode)
// 自动生成,解析字典去生成,有多少key,就有多少个属性
- (void)propertyCode
{
    NSMutableString *codes = [NSMutableString string];
    NSMutableString *codes2 = [NSMutableString string];

    // 遍历字典中所有key
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull value, BOOL * _Nonnull stop) {
        
        NSString *code;
        
        if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
            
            code  = [NSString stringWithFormat:@"var %@ : Bool = false",key];
            
        } else if ([value isKindOfClass:[NSString class]]) {
            
            code  = [NSString stringWithFormat:@"var %@ = \" \"",key];
            
        } else if ([value isKindOfClass:[NSArray class]]) {
            
            code  = [NSString stringWithFormat:@"var %@ = [AnyObject]()",key];
            
        } else if ([value isKindOfClass:[NSDictionary class]]) {
            
            code  = [NSString stringWithFormat:@"var %@ = [String : AnyObject]()",key];
            
        } else if ([value isKindOfClass:[NSNumber class]]) {
            
            code  = [NSString stringWithFormat:@"var %@ = NSNumber(value: -1)",key];
            
        }
        
        // 生成一行属性代码 reposts_count:@property (nonatomic, assign) NSInteger reposts_count;
        
        [codes appendFormat:@"\n%@",code];
        [codes2 appendFormat:@",\"%@\"",key];
        
    }];
    
    [codes2 appendString:@"]"];
    [codes2 replaceCharactersInRange:NSMakeRange(0, 1) withString:@"["];


    
    NSLog(@"%@",codes);
    NSLog(@"%@",codes2);

}

@end
